package com.alfy.sbdemo.websocketdemo.demo1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * client controller
 *
 * @author pengyf
 * @date 2024/05/28
 */
@RestController
public class ClientController {

    @GetMapping("/test")
    public String test() throws Exception {
        WebSocketClient client = new WebSocketClient();
        try {
            String url = "ws://localhost:8889/websocket/username/123?token=valid-token"; // 确保这是正确的 URL
            client.connect(url);
            client.send("Hello, WebSocket Server!");
            Thread.sleep(5000); // 等待接收消息
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
}
